package com.martian.controller;

import com.martian.common.exception.SysException;
import com.martian.common.exception.UserException;
import com.martian.common.util.ThreadLocalMap;
import com.martian.common.wrapper.ResultWrapper;
import com.martian.pojo.entity.MovieCollect;
import com.martian.pojo.form.FrozenUserForm;
import com.martian.pojo.form.UserLogOutForm;
import com.martian.pojo.form.UserLoginForm;
import com.martian.pojo.form.UserRegisterForm;
import com.martian.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.SQLIntegrityConstraintViolationException;

/**
 * @author martian
 * @version V1.0.0
 * @Description
 * @date 2023-03-24 9:52
 */
@CrossOrigin
@Api(value = "UserController", tags = {"用户API"})
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    UserService userService;

    @ApiOperation(value = "用户登录", produces = "application/json")
    @PostMapping("/visitor/userLogin")
    public ResultWrapper userLogin(@RequestBody UserLoginForm userLoginForm){
        try {
            return ResultWrapper.successWithData(userService.userLogin(userLoginForm));
        }catch (SysException e){
            return ResultWrapper.failure(e.getMessage());
        }
    }
    @ApiOperation(value = "用户退出登录", produces = "application/json")
    @PostMapping("/userLogOut")
    public  ResultWrapper userlogOut(@RequestBody UserLogOutForm userLogOutForm){
        try{
            userService.userlogout(userLogOutForm);
            return ResultWrapper.success("退出成功");
        }catch(SysException e){
            return ResultWrapper.failure(e.getMessage());
        }
    }
    @ApiOperation(value = "猜你喜欢", produces = "application/json")
    @GetMapping("/recMovieForYou")
    public ResultWrapper recMovieForYou(@RequestParam Integer size,@RequestParam Integer userId){
        try {
            return ResultWrapper.successWithData(userService.recMovieForYou(size,userId));
        }catch (SysException e){
            return ResultWrapper.failure(e.getMessage());
        }
    }

    @ApiOperation(value = "验证token",produces = "application/json")
    @GetMapping("/validateToken")
    public ResultWrapper validateToken(){
        return ResultWrapper.successWithData(ThreadLocalMap.get("THREAD_LOCAL_KEY_LOGIN_USER"));
    }

    /**
     * @TODO 这里的异常检测存在问题，movieId的范围和格式出错，不能正常返回提示信息。
     * @param movieCollect
     * @return
     */
    @ApiOperation(value = "收藏电影",produces = "application/json")
    @PostMapping("/collectMovie")
    public ResultWrapper collectMovie(@RequestBody MovieCollect movieCollect) throws SQLIntegrityConstraintViolationException {
//        try{
        int status = userService.collectMovie(movieCollect);
        if (status == 1) {
            return ResultWrapper.success("收藏成功");
        }else if(status == 0){
            return ResultWrapper.success("取消收藏");
        }else{
            return ResultWrapper.failure("数据有误");
        }
//        }catch (SQLIntegrityConstraintViolationException e){
//            System.out.println(e.getMessage());
//            return ResultWrapper.failure(e.getMessage());
//        }
    }
    @ApiOperation(value = "查找改用户的已收藏电影",produces = "application/json")
    @GetMapping("/getAllCollectedMovies")
    public ResultWrapper getAllCollectedMovies(@RequestParam Integer page, @RequestParam Integer pageSize, @RequestParam Integer userId){
        try{
            return ResultWrapper.successWithData(userService.getAllCollectedMovies(page,pageSize,userId));
        }catch (SysException e){
            return ResultWrapper.failure(e.getMessage());
        }
    }
    @ApiOperation(value = "返回用户观看历史",produces = "application/json")
    @GetMapping("/getUserRatingHistory")
    public ResultWrapper getUserRatingHistory(@RequestParam Integer page, @RequestParam Integer pageSize,@RequestParam Integer userId){
        try{
            return ResultWrapper.successWithData(userService.getUserRatingHistory(page,pageSize,userId));
        }catch (SysException e){
            return ResultWrapper.failure(e.getMessage());
        }
    }
    @ApiOperation(value = "查询热门用户",produces = "application/json")
    @GetMapping("/visitor/getHotUsers")
    public ResultWrapper getHotUsers(){
        try{
            return ResultWrapper.successWithData(userService.getHotUsers());
        }catch (SysException e){
            return ResultWrapper.failure(e.getMessage());
        }
    }
    @ApiOperation(value = "查询全部用户",produces = "application/json")
    @GetMapping("/getAllUsers")
    public ResultWrapper getAllUsers(@RequestParam Integer page,@RequestParam Integer pageSize){
        try{
            return ResultWrapper.successWithData(userService.getAllUsers(page,pageSize));
        }catch (SysException e){
            return ResultWrapper.failure(e.getMessage());
        }
    }
    @ApiOperation(value = "查询用户浏览历史",produces = "application/json")
    @GetMapping("/getUsersScanHistory")
    public ResultWrapper getUsersScanHistory(@RequestParam Integer page, @RequestParam Integer pageSize){
        try{
            return ResultWrapper.successWithData(userService.getUsersScanHistory(page,pageSize));
        }catch (SysException e){
            return ResultWrapper.failure(e.getMessage());
        }
    }
    @ApiOperation(value = "冻结或解冻用户账户",produces = "application/json")
    @PostMapping("/frozenUsers")
    public ResultWrapper frozenUsers(@RequestBody FrozenUserForm frozenUserForm){
        try{
            userService.frozenUsers(frozenUserForm);
            return ResultWrapper.success();
        }catch (SysException e){
            return ResultWrapper.failure(e.getMessage());
        }
    }

}
